import axios from 'axios';
import qs from 'qs'
import { message } from 'antd';
import config from '../../config'

axios.defaults.timeout = 5000;
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
axios.defaults.baseURL = config.url;

//POST传参序列化
axios.interceptors.request.use((config) => {
  if (config.method === 'post') {
    config.data = qs.stringify(config.data);
  }
  return config;
}, (error) => {
  message.error('错误的传参');
  return Promise.resolve(error);
});

//code状态码200判断
axios.interceptors.response.use((res) => {
  if (res.status !== 200) {
    message.error('请求出错了！');
    return Promise.resolve(res);
  }
  return res;
}, (error) => {
  message.error('网络异常');
  return Promise.resolve(error);
});

export default axios;